/**
 * 商品基础信息查询
 */
var oTable;
var oTableLog;

$('#search').click(function(){
	
	var fromcity = $('#fromcity').val();
	if(fromcity==''||fromcity == '请输入出发城市...'){
		layer.alert("出发城市三字码不能为空", {
						icon:6,
				        closeBtn: 0,
				        shift: 3 //动画类型
				    });
		return false;
	}
	var tocity = $('#tocity').val();
	if(tocity==''||tocity == '请输入到达城市...'){
		layer.alert("到达城市三字码不能为空", {
						icon:6,
				        closeBtn: 0,
				        shift: 3 //动画类型
				    });
		return false;
	}
	var fromdatestart = $('#fromdatestart').val();
	if(fromdatestart==''||fromdatestart=='请选择出发时间起'){
		layer.alert("出发时间起不能为空", {
						icon:6,
				        closeBtn: 0,
				        shift: 3 //动画类型
				    });
		return false;
	}
	var fromdateend = $('#fromdateend').val();
	if(fromdateend==''||fromdateend=='请选择出发时间起'){
		layer.alert("出发时间止不能为空", {
						icon:6,
				        closeBtn: 0,
				        shift: 3 //动画类型
				    });
		return false;
	}
	oTable.ajax.reload();
	
});

oTable = $('#profit').DataTable({
	"dom": '<"top">rt<"bottom"ip><"clear">',
	"ajax": {
		"type": "POST",
		"url": "query.ajax",
		"data": function(d) {
			d.fromdatestart = $('#fromdatestart').val(),
			d.fromdateend = $('#fromdateend').val(),
			d.fromcity = $('#fromcity').val(),
			d.tocity = $('#tocity').val();
			d.flighttype = $(":radio:checked").val();
		}
	},
	"processing": true,
    "serverSide": true,
	"ordering": false,
	"aLengthMenu": [5, 10, 20, 40 ],
	"iDisplayLength" : 10,
	"oLanguage": {
		"sUrl": "../static/js/datatable.cn.txt"
	},
	"aoColumns": [
	    {"mDataProp":"productInfoId"},
	    {"mDataProp":"airline"},
	    {"mDataProp":"fromdateStr"},
	    {"mDataProp":"fromtime"},
	    {"mDataProp":"price"},
	    {"mDataProp":"tax"},
	    {"mDataProp":"total"},
	    {"mDataProp":"threshold1"},
	    {"mDataProp":"channelLowerPrice"},
	    {"mDataProp":"threshold2"},
	    {"mDataProp":"total2"},
	    {"mDataProp":"status"},
	    {"mDataProp":"modify"}
	],
	"columnDefs": [
		{
			"targets": 0,
			"render":function(data,type,row){
				return'<input type="checkbox" name="productInfoId" value="'+row.productInfoId+'">' +row.relateid;
			}
		},
		{
	    	
			"targets": 1,
				"render": function (data, type, row) {
					var ret='';
					if(row.flighttype='oneWay'){
						
						if(row.transferAirport!=null&&row.transferAirport!=''){
							ret = row.fromairport+'-'+row.transferAirport+'</br>'+row.transferAirport+'-'+row.toairport;
						}else{
							ret = row.fromairport+'-'+row.toairport;
						}
						
					}else{
						if(row.transferAirport!=null&&row.transferAirport!=''){
							ret = row.fromairport+'-'+row.transferAirport+'</br>'+row.transferAirport+'-'+row.toairport
							+row.toairport+'-'+row.transferAirport+'</br>'+row.transferAirport+'-'+row.fromairport;
							
						}else{
							ret = row.fromairport+'-'+row.toairport+'</br>'+row.toairport+'-'+row.fromairport;
						}
					}
					return ret;
				}
		}, 
		{
			"targets": 6,
			"render":function(data,type,row){
				return row.price+row.tax;
			}
		
		},
		{
			"targets": 7,
			"render":function(data,type,row){
				var thresholdPercent1 = row.thresholdPercent2==null?0:row.thresholdPercent1; 
				var thresholdAmount1 = row.thresholdAmount2==null?0:row.thresholdAmount1;
				var profitInfoId = row.profitInfoId==null?0:row.profitInfoId;
				return '<input type="text" id="'+row.productInfoId+profitInfoId+'p1" value="'+thresholdPercent1+'" style="width:50px;height:22px">%<input type="text" id="'+row.productInfoId+profitInfoId+'a1" value="'+thresholdAmount1+'" style="width:50px;height:22px">';
			}
		
		},

		{
			"targets": 9,
			"render":function(data,type,row){
				var thresholdPercent2 = row.thresholdPercent2==null?0:row.thresholdPercent2; 
				var thresholdAmount2 = row.thresholdAmount2==null?0:row.thresholdAmount2;
				var profitInfoId = row.profitInfoId==null?0:row.profitInfoId;
				return '<input type="text" id="'+row.productInfoId+profitInfoId+'p2" value="'+thresholdPercent2+'" style="width:50px;height:22px">%<input type="text" id="'+row.productInfoId+profitInfoId+'a2" value="'+thresholdAmount2+'" style="width:50px;height:22px">';
			}
		
		},
		{
			"targets":10,
			"render":function(data,type,row){
				if(row.adultPrice+row.adultTax==0){
					return row.price+row.tax;
				}else{
					return row.adultPrice+row.adultTax;
				}
				
			}
		
		},
        {
			"targets": 11,
            "render": function ( data, type, row ) {
         	   return row.status==0?'':(row.status==4?'已投放':'未投放');
            }
        },
        {
			"targets": 12,
            "render": function ( data, type, row ) {
            	
	        	var ret='';
				if(row.flighttype='oneWay'){
					
					if(row.transferAirport!=null&&row.transferAirport!=''){
						ret = row.fromairport+'-'+row.transferAirport+','+row.transferAirport+'-'+row.toairport;
					}else{
						ret = row.fromairport+'-'+row.toairport;
					}
					
				}else{
					if(row.transferAirport!=null&&row.transferAirport!=''){
						ret = row.fromairport+'-'+row.transferAirport+','+row.transferAirport+'-'+row.toairport
						+row.toairport+'-'+row.transferAirport+','+row.transferAirport+'-'+row.fromairport;
						
					}else{
						ret = row.fromairport+'-'+row.toairport+','+row.toairport+'-'+row.fromairport;
					}
				}
				var profitInfoId = row.profitInfoId==null?0:row.profitInfoId;
         	   var tmp =  '<a class="btn btn-primary btn-xs" href="javascript:void(0);"  onclick="javascript:editProfitInfo(' + row.productInfoId + ','+ profitInfoId+')" title="修改"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改</a>&nbsp;&nbsp;'
         	         +'<a class="btn btn-primary btn-xs" href="javascript:void(0);"  onclick="javascript:queryLog(' + profitInfoId + ',\''+ret+'\')" title="查看日志"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>查看日志</a>';
         	         if(row.profitStatus==4)
         	        	tmp +='<a class="btn btn-primary btn-xs disabled" href="javascript:void(0);" onclick="javascript:msg();" title="投放"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>投放</a>&nbsp;&nbsp;';
         	         else 
         	        	tmp +='<a class="btn btn-primary btn-xs" href="javascript:void(0);"  onclick="javascript:putOn('  + row.productInfoId + ','+ profitInfoId+ ')" title="投放"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>投放</a>&nbsp;&nbsp;';
         	   return tmp;
            }
        }
		
   ]
});


oTableLog = $('#profit_log').DataTable({
	"dom": '<"top">t<"bottom"ip><"clear">',
	"ajax": {
		"type": "POST",
		"url": "queryLogByProfigId.ajax",
		"data": function(d) {
			d.id = $('#profitId').val();
		}
	},
	"processing": true,
    "serverSide": true,
	"ordering": false,
	"aLengthMenu": [5, 10, 20, 40 ],
	"iDisplayLength" : 10,
	"oLanguage": {
		"sUrl": "../static/js/datatable.cn.txt"
	},
	"aoColumns": [
	    {"mDataProp":"id"},
	    {"mDataProp":"msg"},
	    {"mDataProp":"creator"},
	    {"mDataProp":"createtimestr"}
	   
	]
});

